home *** CD-ROM | disk | FTP | other *** search
- Path: news.umbc.edu!not-for-mail
- From: schlein@umbc.edu (Jonas J. Schlein)
- Newsgroups: comp.lang.c
- Subject: Re: Location of an array?
- Date: 13 Jan 1996 15:08:33 -0500
- Organization: University of Maryland Baltimore County
- Message-ID: <4d93g1$bj1@umbc9.umbc.edu>
- References: <4d4iqk$hs3@overload.lbl.gov>
- NNTP-Posting-Host: f-umbc9.umbc.edu
- NNTP-Posting-User: schlein
-
- Mikhail Faiguenblat <mfaiguen> wrote:
- |> I am sure this has been answered a million times by now, but could someody
- |> help me, please?
- |>
- |> I have an array in my program, and I need to find out the absolute location
- |> in memory where this array is located.
- |>
- |> I have tried to do it this way:
- |>
- |> #include <stdio.h>
- |>
- |> int main() {
- |> char arr[10];
- |> int addr;
- |>
- |> printf("sizeof(char *) = %d\n", sizeof(char *));
- |> printf("sizeof(int) = %d\n", sizeof(int));
- |>
- |> addr = arr;
-
- Just because the size is the same doesn't make this assignment legal. My
- compiler refuses to even compile this unless I put an explicit cast.
-
- |> printf("addr = %d\n", addr);
-
- Only 2 types of pointers can be passed to printf(). A char * as in a
- NUL terminated string and a void *. Now with the void * it's hard to
- say what you will get, but probably some hex value that is not necessarily
- the true memory address. I'd suggest doing:
-
- printf ("addr = %p\n", (void *) &arr[0]);
-
- At least that's legal C.
-
- |> return 0;
- |> }
- |>
- |> And it did not work:
-
- <rest deleted>
- --
- "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
-
- Jonas J. Schlein (schlein@gl.umbc.edu)
-